package routers

import (
	"net/http"

	"github.com/labstack/echo"
	"shqsoft.cn/pasnj/base"
)

// GetLrycMaxdryf 获取例如预测最大的导入月份
func GetLrycMaxdryf(c echo.Context) error {
	nf := c.FormValue("NF")
	row := base.DB.Raw("select max(month(DRRQ)) yf from T_JHCW_LRYC_DTIP where year(DRRQ)=?", nf).Row()
	var yf int
	row.Scan(&yf)
	return c.JSON(http.StatusOK, yf)
}

// SetMnlrYc 获取例如预测最大的导入月份
func SetMnlrYc(c echo.Context) error {
	yf := c.FormValue("yf")
	tx := base.DB.Begin()
	err := tx.Exec("EXEC UP_JHCW_SET_MNLR ?", yf).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// JjTzsdRouter 基金销售费用锁定
func JjTzsdRouter(c echo.Context) error {
	nf := c.FormValue("NY")
	tx := base.DB.Begin()
	err := tx.Exec("UPDATE T_INDI_JJSX_FYDR SET ISSD=1 WHERE DTID=?", nf).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// JjTzjsRouter 基金销售费用解锁
func JjTzjsRouter(c echo.Context) error {
	nf := c.FormValue("NY")
	tx := base.DB.Begin()
	err := tx.Exec("UPDATE T_INDI_JJSX_FYDR SET ISSD=0 WHERE DTID=?", nf).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// JjyjtzRouter 基金销售费用调整
func JjyjtzRouter(c echo.Context) error {
	key := c.FormValue("key")
	emid := c.FormValue("emid")
	tx := base.DB.Begin()
	var userNAME string
	var userNO string
	var UTNM string
	row := base.DB.Raw("SELECT EMNO,EMNM,DPNM FROM DIM_USER WHERE EMID=?", emid).Row()
	err := row.Scan(&userNO, &userNAME, &UTNM)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	err = tx.Exec("UPDATE T_INDI_JJSX_FYDR SET KHJL=?,KHJLMC=?,GSZH=? WHERE ID=?", userNO, userNAME, UTNM, key).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// JjTzsdRouter 基金定投费用锁定
func JjTzsd2Router(c echo.Context) error {
	nf := c.FormValue("NY")
	tx := base.DB.Begin()
	err := tx.Exec("UPDATE T_INDI_JJDT_FYDR SET ISSD=1 WHERE DTID=?", nf).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// JjTzjsRouter 基金销售费用解锁
func JjTzjs2Router(c echo.Context) error {
	nf := c.FormValue("NY")
	tx := base.DB.Begin()
	err := tx.Exec("UPDATE T_INDI_JJDT_FYDR SET ISSD=0 WHERE DTID=?", nf).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// JjyjtzRouter 基金定投费用调整
func Jjyjtz2Router(c echo.Context) error {
	key := c.FormValue("key")
	emid := c.FormValue("emid")
	tx := base.DB.Begin()
	var userNAME string
	var userNO string
	var UTNM string
	row := base.DB.Raw("SELECT EMNO,EMNM,DPNM FROM DIM_USER WHERE EMID=?", emid).Row()
	err := row.Scan(&userNO, &userNAME, &UTNM)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	err = tx.Exec("UPDATE T_INDI_JJDT_FYDR SET KHJLDM=?,KHJLMC=?,GSZH=? WHERE ID=?", userNO, userNAME, UTNM, key).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

func BxtztzRouter(c echo.Context) error {
	key := c.FormValue("key")
	zjhm := c.FormValue("zjhm")
	tx := base.DB.Begin()
	var count int
	var flag string
	row := base.DB.Raw("SELECT COUNT(*) FROM T_INDI_CLIE WHERE IDNO=?", zjhm).Row()
	err := row.Scan(&count)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if count > 0 {
		err = tx.Exec("UPDATE T_INDI_BX_SGBL SET TBRZJHM=? WHERE ID=?", zjhm, key).Error
		if err != nil {
			tx.Rollback()
			return err
		}
		flag = "OK"
	} else {
		flag = "NO"
	}
	tx.Commit()
	return c.JSON(http.StatusOK, flag)
}

func ReStart(c echo.Context) error {
	err := base.DB.Exec("EXEC msdb.dbo.sp_start_job 'RESTART'").Error
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	return c.JSON(http.StatusOK, "OK")
}
